//
//  UIBarButtonItem+Extension.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/19.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    convenience init(imageName: String? = nil, title: String? = nil, target: AnyObject?, action: Selector) {
        self.init()
        
        let btn = UIButton()
        if let img = imageName {
            
            btn.setImage(UIImage(named:img) , forState: .Normal)
            btn.setImage(UIImage(named:img + "_highlighted") , forState: .Highlighted)
        }
        
        btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        btn.setTitle(title, forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        //解决真机中navigationBar中文字显示的问题
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 5)
        btn.sizeToFit()
        
        self.customView = btn
        
        
    }

}
